/*******************************************************************************
* 
* PICPROG -  PIC programmer
* Copyright (C) 2009 Olivier Ringot <oringot@gmail.com>
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, 
* Boston, MA  02110-1301, USA.
* 
********************************************************************************
* 
* $Revision: $
* $Date: $
* $Source: $
* $Log: $
*
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

/*******************************************************************************
* send_char
*******************************************************************************/
void send_char(int device,unsigned char c)
{
  int count;
  do
  {
    count=write(device,&c,1);
    if(count==-1)
    {
      printf("error: problem during send_char\n");
      exit(-1);
    }
  }
  while(count==0);
}

/*******************************************************************************
* get_char
*******************************************************************************/
unsigned char get_char(int device)
{
  int count;
  unsigned char r;
  do                                        
  {                                         
    count=read(device,&r,1);                
  }
  while(count==0);
  if(count!=1)
  {
    printf("error: problem during get_char (%d,%d)\n",count,errno);
    perror(NULL);
    exit(-1);
  }
  return r;
}

/*******************************************************************************
* check_ack
*******************************************************************************/
void check_ack(int device)
{
  unsigned char c;
  
  c= get_char(device);
  if(c!=0)
  {
    printf("error: unexpected value for acknowledge !\n");
    exit(-1);
  }
}
